package com.yitong.bmd.services;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yitong.bmd.dao.BmdManagerDao;
import com.yitong.bmd.domain.BmdEntity;
import com.yitong.bmd.domain.PageReults;
import com.yitong.bmd.domain.WhiteInfo;


@Service
@Transactional(value = "secondTxManager", rollbackFor = Exception.class)
class BmdManagerServiceImpl implements BmdManagerService {
	private Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	BmdManagerDao bmdManagerDao;

	@Override
	public PageReults getWhiteUserList(String qynbzh, String ygmc, String sfxh,String monthsalary, String months, String cxlb, int curpageno,int pagesize) {
		logger.info("查询白名单中。。。。");
		PageReults result = new PageReults();
		List<BmdEntity> list = bmdManagerDao.getWhiteUserList(qynbzh,ygmc,sfxh,monthsalary,months,cxlb,curpageno,pagesize);
		Long totalitem = bmdManagerDao.getTotalItemCounts(qynbzh,ygmc,sfxh,monthsalary,months,cxlb);
		result.setList(list);
		result.setTotalitem(totalitem);
		return result;
	}

	@Override
	public String getEntLvl(String ent_cust_id) {
		logger.info("查询企业级别。。。。");
		return bmdManagerDao.getEntLvl(ent_cust_id);
	}

	@Override
	public String insertWhitelist(List<WhiteInfo> list) {
		List<WhiteInfo> left = new ArrayList<WhiteInfo>();
		for(WhiteInfo w:list){
			String isExist=bmdManagerDao.getWhiterBySfz(w.getCust_idt_no());
			if(!isExist.equals("true")){
				String f=bmdManagerDao.insertWhiter(w);
				if(f.equals("false")){
					left.add(w);
				}
			}
		}
		if(left.size()>0){
			return "false";
		}else{
			return "true";
		}
	}

	@Override
	public String deleteWhitelist(List<WhiteInfo> list) {
		List<WhiteInfo> left = new ArrayList<WhiteInfo>();
		for(WhiteInfo w:list){
			String isExist=bmdManagerDao.getWhiterBySfz(w.getCust_idt_no());
			if(isExist.equals("true")){
				String f=bmdManagerDao.deleteWhiter(w);
				if(f.equals("false")){
					left.add(w);
				}
			}
		}
		if(left.size()>0){
			return "false";
		}else{
			return "true";
		}
	}
	
}